package com.lykj.llw.box.ui.activity

import android.content.Context
import android.webkit.WebResourceRequest
import android.os.Bundle
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.llw.box.databinding.ActCommonWebviewBinding


/**
 * ================================================
 * 通用网页加载页面CommonWebViewActivity
 * ================================================
 *
 * @author gosha
 * @date 2022/9/8-16:06
 */
class CommonWebViewActivity : BaseActivity() {
    lateinit var binding: ActCommonWebviewBinding

    private var mUrl: String = ""
    private var mTitle: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActCommonWebviewBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun initUI() {
        if (null != intent) {
            mTitle = intent.getStringExtra("title").toString()
        }
        binding.titleBar.tvTitle.text = mTitle
        binding.titleBar.tvRightOption.visibility = View.INVISIBLE
    }

    override fun initData() {
        if (null != intent) {
            mUrl = intent.getStringExtra("url").toString()
        }

        initWebView()
    }

    private fun initWebView() {
        initWebViewSetting()

        //加载url
        binding.wvContent.loadUrl(mUrl)
    }

    //初始化WebView设置
    private fun initWebViewSetting() {
        //启用js脚本
        binding.wvContent.settings.javaScriptEnabled = true
        //自适应图片
        binding.wvContent.settings.useWideViewPort = true
        //缩放到屏幕大小
        binding.wvContent.settings.loadWithOverviewMode = true
        binding.wvContent.settings.setSupportZoom(true)
        binding.wvContent.settings.builtInZoomControls = true
        binding.wvContent.settings.domStorageEnabled = true
        //显示原生缩放
        binding.wvContent.settings.displayZoomControls = false

        //其他细节操作
        binding.wvContent.settings.cacheMode = WebSettings.LOAD_NO_CACHE
        //设置可以访问文件
        binding.wvContent.settings.allowFileAccess = true
        //支持通过JS打开新窗口
        binding.wvContent.settings.javaScriptCanOpenWindowsAutomatically = true
        //支持自动加载图片
        binding.wvContent.settings.loadsImagesAutomatically = true
        binding.wvContent.settings.defaultTextEncodingName = "utf-8"
//        binding.wvContent.addJavascriptInterface(JsInterface(this), "AndroidWebView")

        binding.wvContent.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                return super.shouldOverrideUrlLoading(view, request)
            }
        }
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()

        binding.wvContent.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding.wvContent.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        //清理缓存
        binding.wvContent.clearCache(false)
        //清除历史记录
        binding.wvContent.clearHistory()
        //清除表单数据
        binding.wvContent.clearFormData()
        binding.wvContent.destroy()
    }

    //JS交互接口
    class JsInterface(context: Context) {
        var mCtx: Context? = null

        init {
            mCtx = context
        }
    }
}